Vue.use(VueResource)
new Vue({
    el: '#app',
    data: {
        userInfo: {
            name: '',
            password: ''
        },
        rule: {
            password: /^[\w]{6,16}$/
        },
        layerOptions: {
            icon: 5,
            time: 1500,
            shade: [0.8, '#eee']
        }
    },
    methods: {
        login: function() {
            var user = this.userInfo
            if (!user.name) {
                return layer.msg('用户名为空！', this.layerOptions)
            }
            if (!user.password) {
                return layer.msg('密码为空！', this.layerOptions)
            }
            if (!this.rule.password.test(user.password)) {
                return layer.msg('密码格式不对！', this.layerOptions)
            }
            this.$http.get('/user/login', {
                params: this.userInfo
            }).then(function(res) {
                if (res.data.code == 100) {
                    layer.msg('登陆成功！', {
                        icon: 6,
                        time: 1000
                    })
                    var user = res.data.data.user
                    setTimeout(function() {
                        localStorage.memoryClassUser = JSON.stringify({
                            id: user.id,
                            name: user.name
                        })
                        location.href = 'http://' + location.host + '/'
                    }.bind(this), 1000)
                } else {
                    layer.msg(res.data.msg, this.layerOptions)
                }
            })
        }
    }
})